
package com.xian.timer.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.xian.timer.R;
import com.xian.timer.fragment.DiscoveryFragment;
import com.xian.timer.fragment.GiftFragment;
import com.xian.timer.fragment.PrivateFragment;
import com.xian.timer.fragment.PublicFragment;
import com.xian.timer.view.DrawerView;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private Fragment publicFragment, privateFragment,
            giftFragment, discoveryFragment;

    private RelativeLayout tabRl1, tabRl2, tabAddRl, tabRl3, tabRl4;

    private FragmentManager mFragmentManager;
    private Context mContext;

    private ImageView titleLeftIv, titleRightIv;
    private SlidingMenu mSlidingMenu;

    private final int currentTab = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mFragmentManager = getSupportFragmentManager();
        setContentView(R.layout.main);
        initView();
        initSlidingMenu();
        initListener();
        setTabSelection(0);

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e("lmf", ">>>>>>>xxxxxxxxxx>>>>>>>>>>>>>>>");
        mSlidingMenu.showContent(false);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    private void initView() {
        titleLeftIv = (ImageView) findViewById(R.id.head_left_iv);
        titleRightIv = (ImageView) findViewById(R.id.head_right_iv);

        tabRl1 = (RelativeLayout) findViewById(R.id.tab_one_rl);
        tabRl2 = (RelativeLayout) findViewById(R.id.tab_two_rl);
        tabAddRl = (RelativeLayout) findViewById(R.id.tab_three_rl);
        tabRl3 = (RelativeLayout) findViewById(R.id.tab_four_rl);
        tabRl4 = (RelativeLayout) findViewById(R.id.tab_five_rl);

    }

    private void initListener() {
        titleLeftIv.setOnClickListener(this);
        titleRightIv.setOnClickListener(this);

        tabRl1.setOnClickListener(this);
        tabRl2.setOnClickListener(this);
        tabAddRl.setOnClickListener(this);
        tabRl3.setOnClickListener(this);
        tabRl4.setOnClickListener(this);
    }

    private void initSlidingMenu() {
        mSlidingMenu = new DrawerView(this).initSlidingMenu();
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mSlidingMenu.isMenuShowing() || mSlidingMenu.isSecondaryMenuShowing()) {
                mSlidingMenu.showContent();
            } else {
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(this, "在按一次退出", Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    finish();
                }
            }
            return true;
        }
        // 拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.head_left_iv:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    mSlidingMenu.showMenu();
                }
                break;
            case R.id.head_right_iv:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    mSlidingMenu.showSecondaryMenu();
                }
                break;

            case R.id.tab_one_rl:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    setTabSelection(0);
                }
                break;
            case R.id.tab_two_rl:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    setTabSelection(1);
                }
                break;
            case R.id.tab_three_rl:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                }
                break;
            case R.id.tab_four_rl:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    setTabSelection(2);
                }
                break;
            case R.id.tab_five_rl:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    setTabSelection(3);
                }
                break;

            default:
                break;
        }
    }

    private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideTabFragments(transaction);
        switch (index) {
            case 0:
                // 当点击了消息tab时，改变控件的图片和文字颜色
                // messageImage.setImageResource(R.drawable.message_selected);
                // messageText.setTextColor(Color.WHITE);
                if (publicFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    publicFragment = new PublicFragment();
                    transaction.add(R.id.main_contain, publicFragment);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(publicFragment);
                }
                break;
            case 1:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                // contactsImage.setImageResource(R.drawable.contacts_selected);
                // contactsText.setTextColor(Color.WHITE);
                if (privateFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    privateFragment = new PrivateFragment();
                    transaction.add(R.id.main_contain, privateFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(privateFragment);
                }
                break;
            case 2:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                // contactsImage.setImageResource(R.drawable.contacts_selected);
                // contactsText.setTextColor(Color.WHITE);
                if (discoveryFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    discoveryFragment = new DiscoveryFragment();
                    transaction.add(R.id.main_contain, discoveryFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(discoveryFragment);
                }
                break;
            case 3:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                // contactsImage.setImageResource(R.drawable.contacts_selected);
                // contactsText.setTextColor(Color.WHITE);
                if (giftFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    giftFragment = new GiftFragment();
                    transaction.add(R.id.main_contain, giftFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(giftFragment);
                }

                break;
        }
        transaction.commit();
    }

    private void clearSelection() {

    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideTabFragments(FragmentTransaction transaction) {
        if (publicFragment != null) {
            transaction.hide(publicFragment);
        }
        if (privateFragment != null) {
            transaction.hide(privateFragment);
        }
        if (discoveryFragment != null) {
            transaction.hide(discoveryFragment);
        }
        if (giftFragment != null) {
            transaction.hide(giftFragment);
        }
    }

}
